package jlibrtpTest;

import java.net.DatagramSocket;
import java.net.InetSocketAddress;

import jlibrtp.*;

import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class TestRTPReceiverThread extends Thread implements RTPAppIntf, DebugAppIntf {

	static long byteCounter = 0;
	static long pktCounter = 0;

	RTPSession rtpSession = null;

	TextView tvRecvData = null;

	private static final String TAG = "TestRecvThread";

	public TestRTPReceiverThread(View view) {
		this.tvRecvData = (TextView) view;
	}

	@Override
	public void receiveData(DataFrame frame, Participant participant) {
		// TODO Auto-generated method stub
		// byte[] data = frame.getConcatenatedData();
		// byteCounter += data.length;
		// pktCounter++;
		//
		// if (pktCounter % 100 == 0) {
		// System.out.print(".");
		// }
		final String s = new String(frame.getConcatenatedData());
		tvRecvData.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				tvRecvData.setText(s);
			}

		});
//		Toast.makeText(main, "Pkt "+pktCounter+" Data Received: \"" + s + "\" From: "
//				+ participant.getCNAME() + "(" + participant.getSSRC() + ")", Toast.LENGTH_SHORT);
		System.out.println("Pkt "+pktCounter+" Data Received: \"" + s + "\" From: "
				+ participant.getCNAME() + "(" + participant.getSSRC() + ")\n");
		pktCounter++;


	}

	@Override
	public void userEvent(int type, Participant[] participant) {
		// TODO Auto-generated method stub

	}

	@Override
	public int frameSize(int payloadType) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		DatagramSocket rtpSocket = null;
		DatagramSocket rtcpSocket = null;
		try {
			rtpSocket = new DatagramSocket(6004);
			rtcpSocket = new DatagramSocket(6005);
		} catch (Exception e) {
			System.out.println("RTPSession failed to obtain port");
		}

		// 2. Create the RTP session
		rtpSession = new RTPSession(rtpSocket, rtcpSocket);
		System.out.println("My CNAME:" + rtpSession.CNAME());
		// 3. Instantiate the application object

		// 4. Add participants we want to notify upon registration
		// a. Hopefully nobody is listening on this port.

		// 5. Register the callback interface, this launches RTCP threads too
		// The two null parameters are for the RTCP and debug interfaces, not
		// use here
		rtpSession.RTPSessionRegister(this, null, null);
		Participant p = new Participant("127.0.0.1", 6002, 6003);
		rtpSession.addParticipant(p);
		System.out.println("Receiving data");

		int i;
		for (i = 0; i < 1000; i++) {
			try {
				sleep(1000);

			} catch (Exception e) {
			}
			if (i % 10 == 0)
				System.out.println("sleeping... "+i+"s");
		}
		rtpSession.endSession();
	}

	@Override
	public void packetReceived(int type, InetSocketAddress socket,
			String description) {
		// TODO Auto-generated method stub
		Log.d(TAG, "SOCKET: "+socket);
		switch (type) {
		case -2:
			Log.d(TAG, "Invalid RTCP packet received");
			break;
		case -1:
			Log.d(TAG, "Invalid RTP packet received");
		case 0:
			Log.d(TAG, "RTP packet received");
		case 1:
			Log.d(TAG, "RTCP packet received");
		default:
			break;
		}
		Log.d(TAG, "DETAILED: "+description);
	}

	@Override
	public void packetSent(int type, InetSocketAddress socket,
			String description) {
		// TODO Auto-generated method stub
		Log.d(TAG, "SOCKET: "+socket);
		switch (type) {
		case 0:
			Log.d(TAG, "RTP unicast packet sent");
			break;
		case 1:
			Log.d(TAG, "RTP multicast packet sent");
			break;
		case 2:
			Log.d(TAG, "RTCP unicast packet sent");
			break;
		case 3:
			Log.d(TAG, "RTCP multicast packet sent");
			break;
		default:
			break;
		}
		Log.d(TAG, "DETAILED: "+description);
	}

	@Override
	public void importantEvent(int type, String description) {
		// TODO Auto-generated method stub
		switch (type) {
		case 0:
			Log.d(TAG, "Session is terminating");
			break;
		case -1:
			Log.d(TAG, "SSRC conflict");
			break;
		default:
			break;
		}
		Log.d(TAG, "DETAILED: "+description);

	}

	/**
	 * @param args
	 */
/*	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DatagramSocket rtpSocket = null;
		DatagramSocket rtcpSocket = null;
		try {
			rtpSocket = new DatagramSocket(6004);
			rtcpSocket = new DatagramSocket(6005);
		} catch (Exception e) {
			System.out.println("RTPSession failed to obtain port");
		}

		// 2. Create the RTP session
		RTPSession rtpSession = new RTPSession(rtpSocket, rtcpSocket);
		System.out.println("My CNAME:" + rtpSession.CNAME());
		// 3. Instantiate the application object
		TestRTPReceiverThread test = new TestRTPReceiverThread(rtpSession);

		// 4. Add participants we want to notify upon registration
		// a. Hopefully nobody is listening on this port.

		// 5. Register the callback interface, this launches RTCP threads too
		// The two null parameters are for the RTCP and debug interfaces, not
		// use here
		rtpSession.RTPSessionRegister(test, null, null);
		Participant p = new Participant("127.0.0.1", 6002, 6003);
		rtpSession.addParticipant(p);
		System.out.println("Receiving data");

		int i;
		for (i = 0; i < 1000; i++) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {
			}

//			if (i % 10 == 0)
//				System.out.print(".");
		}
		rtpSession.endSession();
	}*/

}
